package priv.happy.mealpickpro.util;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;

public class ImgUtilv2 {
    public static final String imageShowAvatarFolder = "http://localhost:8889/img/avatar/";
    public static final String imageSaveAvatarFolder = "avatar/";
    public static final String imageShowFoodFolder = "http://localhost:8889/img/food/";
    public static final String imageShowDefaultFoodPicture = "http://localhost:8889/img/food/default-food.jpg";
    public static final String imageSaveFoodFolder = "food/";


    /**
     * 图片转Base64字符串
     *
     * @param imageFileName
     * @return
     */
    public static String convertImageToBase64Str(String imageFileName) {
        ByteArrayOutputStream baos = null;
        try {
            //获取图片类型
            String suffix = imageFileName.substring(imageFileName.lastIndexOf(".") + 1);
            //构建文件
            File imageFile = new File(imageFileName);
            //通过ImageIO把文件读取成BufferedImage对象
            BufferedImage bufferedImage = ImageIO.read(imageFile);
            //构建字节数组输出流
            baos = new ByteArrayOutputStream();
            //写入流
            ImageIO.write(bufferedImage, suffix, baos);
            //通过字节数组流获取字节数组
            byte[] bytes = baos.toByteArray();
            //获取JDK8里的编码器Base64.Encoder转为base64字符
            return Base64.getEncoder().encodeToString(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     *  JAR 文件的同级目录
     * @param base64String
     * @param imageFileName
     */
    public static void saveBase64ToImage(String base64String, String imageFileName) {
        ByteArrayInputStream bais = null;
        try {
            // 获取图片类型
            String suffix = imageFileName.substring(imageFileName.lastIndexOf(".") + 1);
            // 将 Base64 字符串转为字节数组
            byte[] bytes = Base64.getDecoder().decode(base64String);
            // 构建字节数组输入流
            bais = new ByteArrayInputStream(bytes);
            // 通过 ImageIO 把字节数组输入流转为 BufferedImage
            BufferedImage bufferedImage = ImageIO.read(bais);

            // 获取 JAR 文件的同级目录
            String userDir = System.getProperty("user.dir");
            Path targetPath = Paths.get(userDir, "img", imageFileName).normalize();

            // 确保目标目录存在
            Files.createDirectories(targetPath.getParent());

            // 写入生成文件
            File imageFile = targetPath.toFile();
            ImageIO.write(bufferedImage, suffix, imageFile);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bais != null) {
                try {
                    bais.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}